<?php defined('SYSPATH') or die('No direct script access.');

class HTTP_Exception_404 extends Kohana_HTTP_Exception_404 {
 
    /**
     * Generate a Response for the 404 Exception.
     *
     * The user should be shown a nice 404 page.
     * 
     * @return Response
     */
    public $_config;
    public $lang = 'ru';
    public $postfix;
    public $title;
    public $description;
    public $sitetext;

    public function prepare_page() {
        $this->_config = Kohana::$config->load('main');

        if (!Request::initial()->param('lang') || Request::initial()->param('lang') == 'ru') {
            $this->lang = 'ru';
        } else {
            $this->lang = Request::initial()->param('lang');
            $this->postfix = '_'.$this->lang;
        }

        $this->sitetext = ORM::factory('Sitetext')->getAll($this->postfix);
        $this->title = $this->sitetext['comapny_name']['_value'].' - 404';
        $this->description = $this->sitetext['meta_description']['_value'];
    }

    public function get_response()
    {
        $this->prepare_page();

        $view = View::factory('layout/error', array(
            'title' => $this->title,
            'controller' => Request::initial()->controller(),
            'header' => View::factory('pieces/header', array(
                'sitetext' => $this->sitetext,
                'menu' => ORM::factory('Menu')->getAll($this->postfix),
                'c_url' => $c_uri = Request::detect_uri(),
                'submenu' => ''
            )),
            'content' => View::factory('errors/404', array('message' => $this->getMessage())),
            'footer' => View::factory('pieces/footer', array('sitetext' => $this->sitetext)),
        ));
 
        // Remembering that `$this` is an instance of HTTP_Exception_404
        //$view->message = $this->getMessage();
 
        $response = Response::factory()
            ->status(404)
            ->body($view->render());
 
        return $response;
    }
}